package com.cmdi.opensite.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmdi.opensite.entity.Registration;
import com.cmdi.opensite.mapper.RegistrationMapper;
import com.cmdi.opensite.service.ActivityService;
import com.cmdi.opensite.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 报名服务实现类
 */
@Service
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements RegistrationService {
    
    @Autowired
    private RegistrationMapper registrationMapper;
    
    @Autowired
    private ActivityService activityService;
    
    @Transactional
    @Override
    public boolean registerActivity(Long activityId, Long userId) {
        // 检查是否已报名
        if (hasRegistered(activityId, userId)) {
            return false;
        }
        
        // 创建报名记录
        Registration registration = new Registration();
        registration.setActivityId(activityId);
        registration.setUserId(userId);
        registration.setStatus(0); // 0表示待确认
        registration.setCheckedIn(0); // 0表示未签到
        
        boolean saved = save(registration);
        if (saved) {
            // 更新活动参与人数
            activityService.updateParticipantCount(activityId, 1);
        }
        return saved;
    }
    
    @Transactional
    @Override
    public boolean cancelRegistration(Long id, Long userId) {
        Registration registration = getById(id);
        if (registration != null && registration.getUserId().equals(userId)) {
            boolean deleted = removeById(id);
            if (deleted) {
                // 更新活动参与人数
                activityService.updateParticipantCount(registration.getActivityId(), -1);
            }
            return deleted;
        }
        return false;
    }
    
    @Override
    public boolean hasRegistered(Long activityId, Long userId) {
        return registrationMapper.findByActivityIdAndUserId(activityId, userId) != null;
    }
    
    @Override
    public IPage<Registration> getActivityRegistrations(IPage<Registration> page, Long activityId) {
        return registrationMapper.selectActivityRegistrations(page, activityId);
    }
    
    @Override
    public IPage<Registration> getUserRegistrations(IPage<Registration> page, Long userId) {
        return registrationMapper.selectUserRegistrations(page, userId);
    }
    
    @Override
    public boolean updateRegistrationStatus(Long id, Integer status) {
        Registration registration = new Registration();
        registration.setId(id);
        registration.setStatus(status);
        return updateById(registration);
    }
    
    @Override
    public boolean batchUpdateStatus(List<Long> ids, Integer status) {
        return registrationMapper.batchUpdateStatus(ids, status) > 0;
    }
    
    @Override
    public boolean checkIn(Long id) {
        Registration registration = getById(id);
        if (registration != null && registration.getStatus() == 1) { // 只有已确认的报名才能签到
            registration.setCheckedIn(1);
            registration.setCheckedInTime(new Date());
            return updateById(registration);
        }
        return false;
    }
    
    @Override
    public int getConfirmedCount(Long activityId) {
        return registrationMapper.countConfirmedRegistrations(activityId);
    }
}